const mongoose = require('mongoose');
const config = require('../src/config/config');
const FoodCategory = require('../src/models/foodCategory.model');
const Food = require('../src/models/food.model');

// 连接到MongoDB
mongoose.connect(config.mongoose.url, config.mongoose.options).then(() => {
  console.log('成功连接到MongoDB');
  generateData().then(() => {
    console.log('测试数据生成完成');
    mongoose.disconnect();
  });
}).catch((error) => {
  console.error('MongoDB连接失败', error);
});

// 食物分类数据
const foodCategories = [
  {
    name: '谷物类',
    description: '富含碳水化合物，提供能量的主要来源',
    image: 'https://images.unsplash.com/photo-1568089486102-2bc5482528a2?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    icon: 'fa-wheat-alt'
  },
  {
    name: '蔬菜类',
    description: '富含维生素、矿物质和膳食纤维',
    image: 'https://images.unsplash.com/photo-1540420773420-3366772f4999?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    icon: 'fa-carrot'
  },
  {
    name: '水果类',
    description: '含有丰富的维生素C和抗氧化物',
    image: 'https://images.unsplash.com/photo-1619566636858-adf3ef46400b?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    icon: 'fa-apple-whole'
  },
  {
    name: '肉禽类',
    description: '优质蛋白质的重要来源',
    image: 'https://images.unsplash.com/photo-1607623814075-e51df1bdc82f?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    icon: 'fa-drumstick-bite'
  },
  {
    name: '海鲜类',
    description: '富含优质蛋白质和不饱和脂肪酸',
    image: 'https://images.unsplash.com/photo-1615141982883-c7ad0e69fd62?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    icon: 'fa-fish'
  },
  {
    name: '豆制品',
    description: '植物性蛋白质的优质来源',
    image: 'https://images.unsplash.com/photo-1625943552791-351ee7089927?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    icon: 'fa-seedling'
  },
  {
    name: '坚果类',
    description: '富含健康脂肪和抗氧化物',
    image: 'https://images.unsplash.com/photo-1573851552153-816785fecf4a?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    icon: 'fa-nuts-mixed'
  },
  {
    name: '乳制品',
    description: '钙质的重要来源',
    image: 'https://images.unsplash.com/photo-1628088062854-d1870b4553da?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    icon: 'fa-cheese'
  }
];

// 食物数据 - 每个分类至少3个，共24个
const foods = [
  // 谷物类
  {
    name: '米饭',
    unit: '克',
    calories: 116,
    nutrition: {
      protein: 2.6,
      fat: 0.3,
      carbohydrate: 25.6,
      fiber: 0.4
    },
    image: 'https://images.unsplash.com/photo-1516684732162-798a0062be99?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '全麦面包',
    unit: '片',
    calories: 69,
    nutrition: {
      protein: 3.6,
      fat: 1.1,
      carbohydrate: 12.9,
      fiber: 1.9
    },
    image: 'https://images.unsplash.com/photo-1598373182133-52452f7691ef?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '燕麦片',
    unit: '克',
    calories: 389,
    nutrition: {
      protein: 16.9,
      fat: 6.9,
      carbohydrate: 66.3,
      fiber: 10.6
    },
    image: 'https://images.unsplash.com/photo-1614961233913-a5113a4a34ed?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  
  // 蔬菜类
  {
    name: '西兰花',
    unit: '克',
    calories: 34,
    nutrition: {
      protein: 2.8,
      fat: 0.4,
      carbohydrate: 6.6,
      fiber: 2.6,
      vitamins: {
        C: 89.2,
        K: 102
      }
    },
    image: 'https://images.unsplash.com/photo-1584270354949-c26b0d5b4a0c?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '胡萝卜',
    unit: '克',
    calories: 41,
    nutrition: {
      protein: 0.9,
      fat: 0.2,
      carbohydrate: 9.6,
      fiber: 2.8,
      vitamins: {
        A: 835
      }
    },
    image: 'https://images.unsplash.com/photo-1598170845058-32b9d6a5da37?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '菠菜',
    unit: '克',
    calories: 23,
    nutrition: {
      protein: 2.9,
      fat: 0.4,
      carbohydrate: 3.6,
      fiber: 2.2,
      vitamins: {
        A: 469,
        K: 483
      },
      minerals: {
        iron: 2.7
      }
    },
    image: 'https://images.unsplash.com/photo-1576045057995-568f588f82fb?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  
  // 水果类
  {
    name: '苹果',
    unit: '个',
    calories: 52,
    nutrition: {
      protein: 0.3,
      fat: 0.2,
      carbohydrate: 13.8,
      fiber: 2.4,
      sugar: 10.4
    },
    image: 'https://images.unsplash.com/photo-1619546813926-a78fa6372cd2?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '香蕉',
    unit: '个',
    calories: 89,
    nutrition: {
      protein: 1.1,
      fat: 0.3,
      carbohydrate: 22.8,
      fiber: 2.6,
      sugar: 12.2,
      minerals: {
        potassium: 358
      }
    },
    image: 'https://images.unsplash.com/photo-1571771894821-ce9b6c11b08e?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '橙子',
    unit: '个',
    calories: 47,
    nutrition: {
      protein: 0.9,
      fat: 0.1,
      carbohydrate: 11.8,
      fiber: 2.4,
      sugar: 9.4,
      vitamins: {
        C: 53.2
      }
    },
    image: 'https://images.unsplash.com/photo-1582979512210-99b6a53386f9?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  
  // 肉禽类
  {
    name: '鸡胸肉',
    unit: '克',
    calories: 165,
    nutrition: {
      protein: 31,
      fat: 3.6,
      carbohydrate: 0,
      cholesterol: 85
    },
    image: 'https://images.unsplash.com/photo-1604503468506-a8da13d82791?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '瘦牛肉',
    unit: '克',
    calories: 250,
    nutrition: {
      protein: 26,
      fat: 17,
      carbohydrate: 0,
      cholesterol: 90,
      minerals: {
        iron: 2.6,
        zinc: 4.8
      }
    },
    image: 'https://images.unsplash.com/photo-1588168333986-5078d3ae3976?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '猪里脊',
    unit: '克',
    calories: 143,
    nutrition: {
      protein: 22,
      fat: 5.4,
      carbohydrate: 0,
      cholesterol: 65
    },
    image: 'https://images.unsplash.com/photo-1560781290-7dc94c0f8f4f?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  
  // 海鲜类
  {
    name: '三文鱼',
    unit: '克',
    calories: 208,
    nutrition: {
      protein: 20,
      fat: 13,
      carbohydrate: 0,
      cholesterol: 55,
      minerals: {
        selenium: 40
      }
    },
    image: 'https://images.unsplash.com/photo-1519708227418-c8fd9a32b7a2?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '虾',
    unit: '克',
    calories: 99,
    nutrition: {
      protein: 24,
      fat: 1.7,
      carbohydrate: 0,
      cholesterol: 195
    },
    image: 'https://images.unsplash.com/photo-1565680018392-f415a50c4411?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '鱿鱼',
    unit: '克',
    calories: 92,
    nutrition: {
      protein: 15.8,
      fat: 1.4,
      carbohydrate: 3.1,
      cholesterol: 233
    },
    image: 'https://images.unsplash.com/photo-1565680018361-9d5af0fb75d0?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  
  // 豆制品
  {
    name: '豆腐',
    unit: '克',
    calories: 83,
    nutrition: {
      protein: 8.1,
      fat: 4.8,
      carbohydrate: 2.7,
      calcium: 201
    },
    image: 'https://images.unsplash.com/photo-1546964830-0a0f3292ce88?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '豆浆',
    unit: '毫升',
    calories: 33,
    nutrition: {
      protein: 3.2,
      fat: 1.8,
      carbohydrate: 1.9
    },
    image: 'https://images.unsplash.com/photo-1517281862878-e228b5fa5719?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '腐竹',
    unit: '克',
    calories: 512,
    nutrition: {
      protein: 52.5,
      fat: 29.7,
      carbohydrate: 5.5
    },
    image: 'https://images.unsplash.com/photo-1539748283678-9c798a93ef7d?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  
  // 坚果类
  {
    name: '核桃',
    unit: '克',
    calories: 654,
    nutrition: {
      protein: 15.2,
      fat: 65.2,
      carbohydrate: 13.7,
      fiber: 6.7
    },
    image: 'https://images.unsplash.com/photo-1508061235687-f09fe1a3132a?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '杏仁',
    unit: '克',
    calories: 579,
    nutrition: {
      protein: 21.2,
      fat: 49.9,
      carbohydrate: 21.7,
      fiber: 12.5
    },
    image: 'https://images.unsplash.com/photo-1591311065766-05782b2cc6a0?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '花生',
    unit: '克',
    calories: 567,
    nutrition: {
      protein: 25.8,
      fat: 49.2,
      carbohydrate: 16.1,
      fiber: 8.5
    },
    image: 'https://images.unsplash.com/photo-1567000833363-2d7027ae1382?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  
  // 乳制品
  {
    name: '全脂牛奶',
    unit: '毫升',
    calories: 61,
    nutrition: {
      protein: 3.2,
      fat: 3.3,
      carbohydrate: 4.8,
      calcium: 113
    },
    image: 'https://images.unsplash.com/photo-1563636619-e9143da7973b?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '酸奶',
    unit: '克',
    calories: 63,
    nutrition: {
      protein: 3.5,
      fat: 3.3,
      carbohydrate: 4.7,
      calcium: 121
    },
    image: 'https://images.unsplash.com/photo-1592136957897-b2b6ca21e10d?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  },
  {
    name: '奶酪',
    unit: '克',
    calories: 403,
    nutrition: {
      protein: 25,
      fat: 33.1,
      carbohydrate: 1.3,
      calcium: 721
    },
    image: 'https://images.unsplash.com/photo-1552767059-ce182eda88cc?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80',
    isVerified: true,
    source: '中国食物成分表'
  }
];

// 生成测试数据的函数
async function generateData() {
  try {
    // 清空现有数据
    await FoodCategory.deleteMany({});
    await Food.deleteMany({});
    console.log('已清空现有数据');
    
    // 插入分类数据
    const categoryDocs = await FoodCategory.insertMany(foodCategories);
    console.log(`已插入 ${categoryDocs.length} 条食物分类数据`);
    
    // 建立分类名称与ID的映射
    const categoryMap = {};
    categoryDocs.forEach(cat => {
      categoryMap[cat.name] = cat._id;
    });
    
    // 为每个食物添加对应的分类ID
    const foodsWithCategory = foods.map(food => {
      let categoryName;
      
      if (food.name === '米饭' || food.name === '全麦面包' || food.name === '燕麦片') {
        categoryName = '谷物类';
      } else if (food.name === '西兰花' || food.name === '胡萝卜' || food.name === '菠菜') {
        categoryName = '蔬菜类';
      } else if (food.name === '苹果' || food.name === '香蕉' || food.name === '橙子') {
        categoryName = '水果类';
      } else if (food.name === '鸡胸肉' || food.name === '瘦牛肉' || food.name === '猪里脊') {
        categoryName = '肉禽类';
      } else if (food.name === '三文鱼' || food.name === '虾' || food.name === '鱿鱼') {
        categoryName = '海鲜类';
      } else if (food.name === '豆腐' || food.name === '豆浆' || food.name === '腐竹') {
        categoryName = '豆制品';
      } else if (food.name === '核桃' || food.name === '杏仁' || food.name === '花生') {
        categoryName = '坚果类';
      } else if (food.name === '全脂牛奶' || food.name === '酸奶' || food.name === '奶酪') {
        categoryName = '乳制品';
      }
      
      return {
        ...food,
        categoryId: categoryMap[categoryName]
      };
    });
    
    // 插入食物数据
    const foodDocs = await Food.insertMany(foodsWithCategory);
    console.log(`已插入 ${foodDocs.length} 条食物数据`);
    
    return { categoryDocs, foodDocs };
  } catch (error) {
    console.error('生成测试数据失败', error);
    throw error;
  }
} 